﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static _01_Tower2._0.CombatSystem;

namespace _01_Tower2._0
{
    internal class CombatSystem
    {
        public enum Selected
        {
            Attack,
            Skill,
            Item,
            Escape,
            Max
        }

        // 战斗系统
        // 触发逻辑
        // 遇到敌人 -> 打开系统 -> 传入双方数据
        // 战斗流程 按照行动速度发动攻击
        // 按照回合轮流交替
        bool start, turn;
        Character attacker, defender;
        Selected selected;
        Random rand;
        Game game;
        bool menuIsOpen;
        int menuX, menuY;
        Hero hero;

        public CombatSystem(Game game)
        {
            this.game = game;
            start = false;
            turn = true;
            selected = Selected.Attack;
            rand = new Random();
            menuIsOpen = false;
            menuX = 60;
            menuY = 22;
        }

        public void StartCombat(Character attacker, Character defender)
        {
            game.scene = Game.Scene.Attack;
            start = true;
            this.attacker = attacker;
            this.defender = defender;
        }

        public void ShowInfo(Character character, int x, int y)
        {
            PrintFrame(8, 12, x, y);
            Utility.MyPrint(character.Name, x + 1, y + 1);
            Utility.MyPrint("------", x + 1, y + 2);
            Utility.MyPrint($"Hp : {character.Hp}", x + 1, y + 3);
            Utility.MyPrint($"Mp : {character.Mp}", x + 1, y + 4);
            Utility.MyPrint($"Atk: {character.Atk}", x + 1, y + 5);
            Utility.MyPrint($"Def: {character.Def}", x + 1, y + 6);
        }

        public void ShowMenu()
        {
            if (!menuIsOpen) return;
            // 外框
            PrintFrame(8, 23, menuX, menuY, ConsoleColor.White, false);

            // 内框
            PrintFrame(3, 10, menuX + 1, menuY + 1, selected == Selected.Attack ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("攻击", menuX + 4, menuY + 2, selected == Selected.Attack ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 12, menuY + 1, selected == Selected.Skill ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("技能", menuX + 15, menuY + 2, selected == Selected.Skill ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 1, menuY + 4, selected == Selected.Item ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("道具", menuX + 4, menuY + 5, selected == Selected.Item ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 12, menuY + 4, selected == Selected.Escape ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("逃跑", menuX + 15, menuY + 5, selected == Selected.Escape ? ConsoleColor.Red : ConsoleColor.White);
        }

        void ClearMenu()
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 23; j++)
                {
                    Utility.MyPrint("  ", menuX + j, menuY + i);
                }
            }
        }

        public void Input()
        {
            if (InputSystem.Instance == null) return;

            // 当菜单是打开状态才能操作
            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                selected -= 2; // 每行相差1个列数
                if (selected < 0)
                {
                    selected += 2 * 2; // -1行和最后一行正好相差行数*列数
                }
            }
            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                selected += 2; // 每行相差1个列数
                if (selected > Selected.Max - 1)
                {
                    selected -= 2 * 2; // -1行和最后一行正好相差行数*列数
                }
            }
            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.A))
            {
                int line = (int)selected / 2;
                selected--; // 每行相差1个列数
                if ((int)selected < line * 2) // 当前选项小于当前行最左边的数 即行号*列数
                {
                    selected += 2; // -1列和最后一列正好相差列数
                }
            }
            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.D))
            {
                int line = (int)selected / 2;
                selected++; // 每行相差1个列数
                if ((int)selected > (line + 1) * 2 - 1) // 大于当前行最右边的数 即(行号+1)*列数-1
                {
                    selected -= 2; // -1列和最后一列正好相差列数
                }
            }

            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.Enter))
            {
                switch (selected)
                {
                    case Selected.Attack:
                        Attack();
                        break;
                    case Selected.Skill:
                        Skill();
                        break;
                    case Selected.Item:
                        Item();
                        break;
                    case Selected.Escape:
                        Escape();
                        break;
                }
            }
        }

        void Attack()
        {
            if (turn)
            {
                attacker.Attack(defender);
            }
            else
            {
                defender.Attack(attacker);
            }
            // 关闭菜单
            menuIsOpen = false;
            ClearMenu();
            // 切换回和
            turn = !turn;
        }

        void Skill()
        {
            menuIsOpen = false;
            ClearMenu();
            turn = !turn;
        }

        void Item()
        {
            menuIsOpen = false;
            ClearMenu();
            turn = !turn;
        }

        void Escape()
        {
            int num = rand.Next(100);
            menuIsOpen = false;
            ClearMenu();
            if (num > 70)
            {
                // 打印 逃跑成功！
                InfoSystem.Instance.AddInfo((turn ? attacker.Name : defender.Name) + "逃跑成功", ConsoleColor.Green);
                game.scene = Game.Scene.Map1;
                start = false;
            }
            else
            {
                // 打印 逃跑失败！
                InfoSystem.Instance.AddInfo((turn ? attacker.Name : defender.Name) + "逃跑失败", ConsoleColor.Red);
                turn = !turn;
            }
        }


        public void Update()
        {
            // 如果系统没有打开 就不进行逻辑更新
            if (!start)
            {
                return;
            }

            // 菜单逻辑
            //if ()

            // 回合切换逻辑
            switch (turn)
            {
                case true:
                    // 进攻方逻辑
                    // 打开菜单 后面再讨论敌人AI 先统一都打开
                    if (attacker.IsDead)
                    {
                        game.scene = Game.Scene.Map1;
                        start = false;
                        break;
                    }
                    if (!menuIsOpen)
                    {
                        menuIsOpen = true;
                        selected = Selected.Attack;
                    }
                    break;
                case false:
                    // 防守方逻辑
                    if (defender.IsDead)
                    {
                        game.scene = Game.Scene.Map1;
                        start = false;
                        break;
                    }
                    if (!menuIsOpen)
                    {
                        menuIsOpen = true;
                        selected = Selected.Attack;
                    }
                    break;
            }
            ShowInfo(attacker, 60, 14);
            ShowInfo(defender, 72, 14);


            //Thread.Sleep(100);
        }

        
        public void Render()
        {
            ShowMenu();
        } 


        void PrintFrame(int row, int col, int x, int y, ConsoleColor color = ConsoleColor.Red, bool clear = true)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    else
                    {
                        temp = "  ";
                    }
                    Utility.MyPrint(temp, x + j, y + i);
                }
            }
        }
    }
}
